<?php

/**
 * turnos actions.
 *
 * @package    SistemaRutasEc
 * @subpackage turnos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class turnosActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->bu1 = $request->getParameter('buspartida');
        $this->co1 = $request->getParameter('id_co');
        $this->me1 = $request->getParameter('mes');
        $this->an1 = $request->getParameter('anio');
        
        
        $this->businicial = $request->getParameter('buspartida');
        $this->idco = $request->getParameter('id_co');
        
        $q = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $this->idco);
        $this->coop = $q->fetchOne();

        $this->rutas = Doctrine::getTable('ruta')->createQuery('r')
                ->where('r.id_co = ?', $this->idco)
                ->execute();
        //buses viejos
        $this->buses = Doctrine::getTable('bus')->createQuery('b')
                ->where('b.id_co = ?', $this->idco)
                ->orderBy('b.disco DESC')
                ->execute();

        $this->vectorbus = new ArrayObject();

        $mesactual = $request->getParameter('mes');
        $anioactual = $request->getParameter('anio');
        $this->mes = $mesactual;
        $this->anio = $anioactual;
        $cob = 0;
        foreach ($this->buses as $bus):

            $fbus = explode("-", $bus->getFechain());
            $afb = $fbus[0];
            $mib = $fbus[1];
            $mescon = 0;
            $aux = $afb;
            $imes = 0;
            $medmes = 0;
            $fmes = 0;
            $tomes = 0;
            $cmes = 0;
            if ($afb == $anioactual) {
                if ($mib == $mesactual) {
                    $cmes = 1;
                } else {
                    for ($mc = $mib; $mc <= $mesactual; $mc++) {
                        $cmes++;
                    } $cmes;
                }$tomes = $cmes;
            } else {
                for ($mc = $mib; $mc <= 12; $mc++) {
                    $imes++;
                }
                $mc = 13;
                if ($aux + 1 == $anioactual) {
                    if ($mc == 13) {
                        for ($mc = 1; $mc <= $mesactual; $mc++) {
                            $fmes++;
                        }
                    }$tomes = $fmes + $imes;
                } else {
                    while ($aux + 1 != $anioactual) {
                        if ($mc == 13) {
                            for ($mc = 1; $mc <= 12; $mc++) {
                                $medmes++;
                            }
                        }
                        $aux++;
                        $mc = 13;
                    }
                }
                if ($mc == 13) {
                    for ($mc = 1; $mc <= $mesactual; $mc++) {
                        $fmes++;
                    }
                }
                $tomes = $imes + $medmes + $fmes;
            }

            if ($tomes > 36) {
                $this->vectorbus[$cob] = $bus;
                $cob++;
            }
            $tomes = 0;
        endforeach;
        $this->vectorbusfinal = new ArrayObject();
        $indicebusfinal = 0;   
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() > 100){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }        
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() < 100&&$this->vectorbus[$i]->getDisco() >= 10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        } 
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() <10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }
        
        $this->vector = new ArrayObject();
        $coun = 0;
        foreach ($this->rutas as $rut):
            $freaux = Doctrine::getTable('frecuencia')->createQuery('f')
                    ->where('f.id_ru = ?', $rut->getIdRu())
                    ->andwhere('f.especial = ?', 0)
                    ->execute();
            foreach ($freaux as $f):
                $this->vector[$coun] = $f;
                $coun++;
            endforeach;
        endforeach;
        $total = count($this->vector);
        $this->vectorfinal = new ArrayObject();
        $menor = 0;
        $i = 0;
        while ($i <= $total) {
            foreach ($this->vector as $frau):
                if ($frau->getPrioridad() == $i) {
                    $this->vectorfinal[$i] = $frau;
                }
            endforeach;
            $i++;
        }
        
    }

    public function executeIndexxls(sfWebRequest $request) {
        $this->businicial = $request->getParameter('buspartida');
        $this->idco = $request->getParameter('id_co');
        $q = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $this->idco);
        $this->coop = $q->fetchOne();

        $this->rutas = Doctrine::getTable('ruta')->createQuery('r')
                ->where('r.id_co = ?', $this->idco)
                ->execute();
        //buses viejos
        $this->buses = Doctrine::getTable('bus')->createQuery('b')
                ->where('b.id_co = ?', $this->idco)
                ->orderBy('b.disco DESC')
                ->execute();

        $this->vectorbus = new ArrayObject();

        $mesactual = $request->getParameter('mes');
        $anioactual = $request->getParameter('anio');
        $this->mes = $mesactual;
        $this->anio = $anioactual;
        $cob = 0;
        foreach ($this->buses as $bus):

            $fbus = explode("-", $bus->getFechain());
            $afb = $fbus[0];
            $mib = $fbus[1];
            $mescon = 0;
            $aux = $afb;
            $imes = 0;
            $medmes = 0;
            $fmes = 0;
            $tomes = 0;
            $cmes = 0;
            if ($afb == $anioactual) {
                if ($mib == $mesactual) {
                    $cmes = 1;
                } else {
                    for ($mc = $mib; $mc <= $mesactual; $mc++) {
                        $cmes++;
                    } $cmes;
                }$tomes = $cmes;
            } else {
                for ($mc = $mib; $mc <= 12; $mc++) {
                    $imes++;
                }
                $mc = 13;
                if ($aux + 1 == $anioactual) {
                    if ($mc == 13) {
                        for ($mc = 1; $mc <= $mesactual; $mc++) {
                            $fmes++;
                        }
                    }$tomes = $fmes + $imes;
                } else {
                    while ($aux + 1 != $anioactual) {
                        if ($mc == 13) {
                            for ($mc = 1; $mc <= 12; $mc++) {
                                $medmes++;
                            }
                        }
                        $aux++;
                        $mc = 13;
                    }
                }
                if ($mc == 13) {
                    for ($mc = 1; $mc <= $mesactual; $mc++) {
                        $fmes++;
                    }
                }
                $tomes = $imes + $medmes + $fmes;
            }

            if ($tomes > 36) {
                $this->vectorbus[$cob] = $bus;
                $cob++;
            }
            $tomes = 0;
        endforeach;
        $this->vectorbusfinal = new ArrayObject();
        $indicebusfinal = 0;   
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() > 100){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }        
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() < 100&&$this->vectorbus[$i]->getDisco() >= 10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        } 
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() <10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }
        
        $this->vector = new ArrayObject();
        $coun = 0;
        foreach ($this->rutas as $rut):
            $freaux = Doctrine::getTable('frecuencia')->createQuery('f')
                    ->where('f.id_ru = ?', $rut->getIdRu())
                    ->andwhere('f.especial = ?', 0)
                    ->execute();
            foreach ($freaux as $f):
                $this->vector[$coun] = $f;
                $coun++;
            endforeach;
        endforeach;
        $total = count($this->vector);
        $this->vectorfinal = new ArrayObject();
        $menor = 0;
        $i = 0;
        while ($i <= $total) {
            foreach ($this->vector as $frau):
                if ($frau->getPrioridad() == $i) {
                    $this->vectorfinal[$i] = $frau;
                }
            endforeach;
            $i++;
        }
    }

    public function executeIndexods(sfWebRequest $request) {
        $this->businicial = $request->getParameter('buspartida');
        $this->idco = $request->getParameter('id_co');
        $q = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $this->idco);
        $this->coop = $q->fetchOne();

        $this->rutas = Doctrine::getTable('ruta')->createQuery('r')
                ->where('r.id_co = ?', $this->idco)
                ->execute();
        //buses viejos
        $this->buses = Doctrine::getTable('bus')->createQuery('b')
                ->where('b.id_co = ?', $this->idco)
                ->orderBy('b.disco DESC')
                ->execute();

        $this->vectorbus = new ArrayObject();

        $mesactual = $request->getParameter('mes');
        $anioactual = $request->getParameter('anio');
        $this->mes = $mesactual;
        $this->anio = $anioactual;
        $cob = 0;
        foreach ($this->buses as $bus):

            $fbus = explode("-", $bus->getFechain());
            $afb = $fbus[0];
            $mib = $fbus[1];
            $mescon = 0;
            $aux = $afb;
            $imes = 0;
            $medmes = 0;
            $fmes = 0;
            $tomes = 0;
            $cmes = 0;
            if ($afb == $anioactual) {
                if ($mib == $mesactual) {
                    $cmes = 1;
                } else {
                    for ($mc = $mib; $mc <= $mesactual; $mc++) {
                        $cmes++;
                    } $cmes;
                }$tomes = $cmes;
            } else {
                for ($mc = $mib; $mc <= 12; $mc++) {
                    $imes++;
                }
                $mc = 13;
                if ($aux + 1 == $anioactual) {
                    if ($mc == 13) {
                        for ($mc = 1; $mc <= $mesactual; $mc++) {
                            $fmes++;
                        }
                    }$tomes = $fmes + $imes;
                } else {
                    while ($aux + 1 != $anioactual) {
                        if ($mc == 13) {
                            for ($mc = 1; $mc <= 12; $mc++) {
                                $medmes++;
                            }
                        }
                        $aux++;
                        $mc = 13;
                    }
                }
                if ($mc == 13) {
                    for ($mc = 1; $mc <= $mesactual; $mc++) {
                        $fmes++;
                    }
                }
                $tomes = $imes + $medmes + $fmes;
            }

            if ($tomes > 36) {
                $this->vectorbus[$cob] = $bus;
                $cob++;
            }
            $tomes = 0;
        endforeach;
        $this->vectorbusfinal = new ArrayObject();
        $indicebusfinal = 0;   
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() > 100){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }        
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() < 100&&$this->vectorbus[$i]->getDisco() >= 10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        } 
        for ($i = 0; $i < count($this->vectorbus); $i++) {
            if($this->vectorbus[$i]->getDisco() <10){
                 $this->vectorbusfinal[$indicebusfinal]=$this->vectorbus[$i];
                    $indicebusfinal++;
            }
        }
        
        $this->vector = new ArrayObject();
        $coun = 0;
        foreach ($this->rutas as $rut):
            $freaux = Doctrine::getTable('frecuencia')->createQuery('f')
                    ->where('f.id_ru = ?', $rut->getIdRu())
                    ->andwhere('f.especial = ?', 0)
                    ->execute();
            foreach ($freaux as $f):
                $this->vector[$coun] = $f;
                $coun++;
            endforeach;
        endforeach;
        $total = count($this->vector);
        $this->vectorfinal = new ArrayObject();
        $menor = 0;
        $i = 0;
        while ($i <= $total) {
            foreach ($this->vector as $frau):
                if ($frau->getPrioridad() == $i) {
                    $this->vectorfinal[$i] = $frau;
                }
            endforeach;
            $i++;
        }
    }

    public function executeShow(sfWebRequest $request) {
        $this->ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru')));
        $this->forward404Unless($this->ruta);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new rutaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new rutaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->form = new rutaForm($ruta);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->form = new rutaForm($ruta);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $ruta->delete();

        $this->redirect('turnos/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $ruta = $form->save();

            $this->redirect('turnos/edit?id_ru=' . $ruta->getIdRu());
        }
    }

}

